#include "soundTask.h"

#include "FreeRTOS.h"
#include "task.h"

#include "nxbot.h"

extern xTaskHandle soundHandle;

static void soundTask(void *pvParameters);

void executeSoundTask(globals_t *pvParameter)
{
	xTaskCreate(soundTask,(const signed portCHAR * const) "soundTask",soundTask_stackSize,(void *)pvParameter,tskIDLE_PRIORITY+2, &soundHandle);
}


//Tarea para solo actualizar las vels en la esctructura global
static void soundTask(void *pvParameters)
{	
	//convertion of parameters 
	globals_t *globals;
	globals=(globals_t *)pvParameters;

	unsigned int freq;
	unsigned char songEnd;
	portTickType actualTick;
	Jingle jing;
	
	
	
	
	for(;;)
	{		
	
		
		
		switch(globals->soundJingle)
		{
			case 0:
			jing = J_CONN;
			break;
			case 1:
			jing = J_DISCONN;
			break;
			case 2:
			jing = J_BATT;
			break;
			
		}
		songEnd = sound_playJingle(&freq, jing);
				
		
		//convert time in ms to frequency
		freq *= (configTICK_RATE_HZ/1000);
		
		if(songEnd)
		{
			//time delay according to the note time
			actualTick = xTaskGetTickCount();
			vTaskDelayUntil( &actualTick,(portTickType)freq);		
		}
		else
		{
			vTaskSuspend( NULL );
		}
	}
}

